package decisionLines.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import decisionLines.control.ChoiceController;
import decisionLines.control.DrawEdgeController;
import decisionLines.model.DecisionLinesEvent;

/**
 * Class for the overall GUI
 */
public class Application extends JFrame {

	private JPanel contentPane;
	
	/** Decision Lines Event to be viewed in this frame */
	DecisionLinesEvent event;
	
	/** Canvas area in which to draw the Lines and Edges */
	DrawingCanvas drawing;
	
	/** Text boxes to accept inputs for and to display choices */
	JTextField[] textFields;

	/** 
	 * Constructor: Create the frame.
	 * @param event -  DecisionLinesEvent, the Decision Lines event 
	 */
	public Application(DecisionLinesEvent event) {
		// set the text field dimensions
		int textFieldWidth = 90;
		int textFieldHeight = 20;
		
		// set the frame dimensions; width based on the number of choices
		int frameWidth = 25 + event.getNumChoices() * (textFieldWidth + 10);
		int frameHeight = 500;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, frameWidth, frameHeight);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		// display the text boxes for choices on the frame
		textFields = new JTextField[event.getNumChoices()];
		//Add the text fields based on the number of choices
		for (int i=0; i < event.getNumChoices(); i++) {
			textFields[i] = new JTextField();
			textFields[i].setColumns(10);
			textFields[i].setBounds(10+(textFieldWidth + 10)*i, 10, textFieldWidth, textFieldHeight);
			
			// register event listener to the text field
			textFields[i].addActionListener(new ChoiceController(event, i, this));
			
			contentPane.add(textFields[i]);
		}
		
		// display the lines and set mouse listener to capture edge requests
		drawing = new DrawingCanvas(event);
		drawing.addMouseListener(new DrawEdgeController(event, this));
		drawing.setBounds(0, 40, frameWidth, frameHeight);
		contentPane.add(drawing);
	}
	
	/**
	 * Method: Get the array of text fields.
	 * @return textFields - the array of text fields representing the choices.
	 */
	public JTextField[] getTextFields(){
		return textFields;
	}
	
	/** 
	 * Method: To redraw the frame we repaint the internal boxes. 
	 */
	public void redraw() {
		drawing.repaint();
	}

	/**	
	 * Method: Return the drawing canvas
	 */
	public DrawingCanvas getDrawingCanvas() {
		return drawing;
	}
}
